<extend name="admin/main"/>

<block name="title">
    <title>新闻列表</title>
</block>

<block name="css"></block>

<block name="heading">新闻列表</block>

<block name="content">
    <form class="form" action="/admin/news/list">
        <div class="form-group">
            <div class="col-md-4">
                <input type="text" id="search" name="search" value="{$search}" class="form-control">
            </div>
            <button class="btn btn-success">搜索</button>
        </div>
    </form>

    <table class="table table-bordered table-hover">
        <tr>
            <th class="text-center text-uppercase">新闻编号</th>
            <th class="text-center text-uppercase">所属栏目</th>
            <th class="text-center text-uppercase">新闻标题</th>
            <th class="text-center text-uppercase">新闻图片</th>
            <th class="text-center text-uppercase">更新时间</th>
            <th class="text-center text-uppercase">发布时间</th>
            <th class="text-center text-uppercase">新闻更新</th>
            <th class="text-center text-uppercase">删除新闻</th>
        </tr>
        <foreach name="data" item="item" key="key">
            <tr>
                <td class="text-center">{$item.id}</td>
                <td class="text-center">{$item.category}</td>
                <td class="text-center">{$item.title}</td>
                <td class="text-center"><img src="{$item.image}" height="30px"></td>
                <td class="text-center">{$item.updated_at}</td>
                <td class="text-center">{$item.created_at}</td>
                <td class="text-center"><a href="/admin/news/list?id={$item.id}">更改</a></td>
                <td class="text-center"><a href="/admin/news/new_delete?id={$item.id}">删除</a></td>
            </tr>
        </foreach>
        <tfoot>
        <tr>
            <td colspan="8">
                {$news->link([])}
            </td>
        </tr>
        </tfoot>
    </table>

    <!--新闻更新-->
    <notempty name="new">
        <div class="col-md-12">
            <div class="panel panel-primary">
                <div class="panel-heading">新闻更新</div>
                <div class="panel-body">
                    <form class="form-horizontal" method="post" action="/admin/News/updates"
                          enctype="multipart/form-data">
                        <input type="hidden" name="id" value="{$new['id']}">
                        <div class="form-group mb-md">
                            <label for="new_categories_id" class="control-label">所属分类</label>
                            <div class="">
                                <select class="form-control" id="new_categories_id" name="new_categories_id" required>
                                    <foreach name="categories" item="category">
                                        <?php $checked = $category['id'] == $new['new_categories_id'] ? 'selected' : ''?>
                                        <option value="{$category.id}" {$checked}>{$category.title}</option>
                                    </foreach>
                                </select>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label for="title" class="control-label">新闻标题</label>
                            <div class="">
                                <input type="text" class="form-control" id="title" name="title" required
                                       value="{$new.title}">
                            </div>
                        </div>

                        <div class="form-group mb-md">
                            <label for="image" class="control-label">新闻图片</label>
                            <div class="">
                                <input type="file" class="form-control" id="image" name="image" required
                                       value="{$new.src}">
                            </div>
                        </div>

                        <div class="form-group mb-md">
                            <label for="contents" class=" control-label">新闻描述</label>
                            <div>
                            <textarea class="form-control" id="contents" name="content"
                                      required>{$new.content}</textarea>
                            </div>
                        </div>

                        <div class="form-group mb-md">
                            <label for="content" class=" control-label">新闻详情</label>
                            <div>
                            <textarea class="form-control" id="content" name="news_content" required
                            ></textarea>
                            </div>
                        </div>

                        <div class="form-group mb-md  pull-right">
                            <button class="btn btn-block btn-success" type="submit">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </notempty>
</block>

<block name="js">
    <script type="text/javascript" src="__PUBLIC__/assets/demo/demo.js"></script>
    <script type="text/javascript" src="__PUBLIC__/assets/demo/demo-switcher.js"></script>
    <script type="text/javascript" src="__PUBLIC__/assets/plugins/bootbox/bootbox.js"></script>

    <script type="text/javascript"
            src="__PUBLIC__/assets/plugins/form-pagedown/js/jquery.pagedown-bootstrap.combined.js">
    </script>
    <script>
        $(document).ready(function () {
            $("textarea#content").pagedownBootstrap();
            $('.wmd-preview').addClass('well');
        });
    </script>

    <script>
        $(document).ready(function () {
            $('.delete').click(function (e) {
                var $this = $(this);
                bootbox.confirm("是否删除？", function (result) {
                    if (result === true) {
                        deleteUser($this.attr('data-id'));
                    }
                });
            });
        });

        function deleteUser(id) {
            ajax({id: id}, function (res) {
                if (res.success === true) {
                    window.location.reload();
                } else {
                    new PNotify({
                        title: 'Oh On!',
                        text: res.data,
                        type: 'error',
                        icon: 'ti ti-info-alt',
                        styling: 'fontawesome'
                    });
                }
            });
        }


        function ajax(data, callback) {
            var settings = {
                "async": true,
                "crossDomain": true,
                "url": "/admin/adminUser/delete",
                "method": "POST",
                "headers": {
                    "content-type": "application/x-www-form-urlencoded"
                },
                "data": data
            };

            $.ajax(settings).done(function (response) {
                callback(JSON.parse(response));
            });
        }

    </script>
</block>
